﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ComponentFactory.Krypton.Toolkit;
using SceneEngine;

namespace SceneEditor.UI
{
    public partial class AddAssetForm : KryptonForm
    {
        private List<string> filePaths = new List<string>();
        private List<string> fullFilePaths = new List<string>();

        private string baseImportPath;

        public List<string> FilePaths
        {
            get { return filePaths; }
        }
        public List<string> FullFilePaths
        {
            get { return fullFilePaths; }
        }

        public string BaseImportPath
        {
            get { return baseImportPath; }
            set { baseImportPath = value; }
        }

        public AddAssetForm()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
                {
                    string directory = Path.GetDirectoryName(openFileDialog1.FileNames[i]);
                    string fileNameWithExtension = Path.GetFileName(openFileDialog1.FileNames[i]);
                    string fullPath = Path.Combine(directory, fileNameWithExtension);

                    if (filePaths.Count > 0)
                    {
                        if (filePaths.Contains(fullPath))
                        {
                            MessageBox.Show(fileNameWithExtension + " has already been added to the list", "File Already Added", MessageBoxButtons.OK);
                            continue;
                        }
                    }

                    ModelManager assetManager = EditorGlobals.ModelManager;

                    if (assetManager.Models.Keys.Contains(Path.Combine(baseImportPath, 
                        Path.GetFileNameWithoutExtension(fullPath))))
                    {
                        MessageBox.Show(fileNameWithExtension + " or a model with " +
                            "the same name already exists in the Models folder");

                        continue;
                    }

                    filePaths.Add(Path.GetFileNameWithoutExtension(fullPath));
                    fullFilePaths.Add(fullPath);

                    lbFiles.Items.Add(fileNameWithExtension);
                    lbFiles.SelectedIndex = lbFiles.Items.Count - 1;
                }
            }
        }

        private void btnImportFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();

            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                string[] exts = new[] { ".fbx", ".x" };
                string[] files = GetFiles(folderDialog.SelectedPath, exts, SearchOption.AllDirectories).ToArray();

                for (int i = 0; i < files.Length; i++)
                {
                    string directory = Path.GetDirectoryName(files[i]);
                    string fileNameWithExtension = Path.GetFileName(files[i]);
                    string fullPath = Path.Combine(directory, fileNameWithExtension);

                    string relativePath = fullPath.Remove(0, folderDialog.SelectedPath.Length + 1);
                    relativePath = Path.ChangeExtension(relativePath, null);

                    if (fullFilePaths.Count > 0)
                    {
                        if (fullFilePaths.Contains(fullPath))
                        {
                            MessageBox.Show(fileNameWithExtension + " has already been added to the list", 
                                "File Already Added", MessageBoxButtons.OK);

                            continue;
                        }
                    }

                    ModelManager assetManager = EditorGlobals.ModelManager;

                    if (assetManager.Models.Keys.Contains(Path.Combine(baseImportPath, relativePath)))
                    {
                        MessageBox.Show(fileNameWithExtension + " or a model with " +
                            "the same name already exists in the Models folder");

                        continue;
                    }

                    filePaths.Add(relativePath);
                    fullFilePaths.Add(fullPath);

                    lbFiles.Items.Add(relativePath);
                    lbFiles.SelectedIndex = lbFiles.Items.Count - 1;
                }
            }
        }

        private static IEnumerable<string> GetFiles(string sourceFolder, string[] exts, System.IO.SearchOption searchOption)
        {
            return System.IO.Directory.GetFiles(sourceFolder, "*.*", searchOption)
                    .Where(s => exts.Contains(System.IO.Path.GetExtension(s), StringComparer.OrdinalIgnoreCase));
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            if (filePaths.Count > 0)
                this.DialogResult = DialogResult.OK;
            else
                this.DialogResult = DialogResult.Cancel;

            this.Close();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbFiles.Items.Count > 0 && lbFiles.SelectedItems.Count > 0)
            {
                for (int i = lbFiles.SelectedItems.Count - 1; i >= 0; i--)
                {
                    filePaths.RemoveAt(lbFiles.Items.IndexOf(lbFiles.SelectedItems[i]));
                    lbFiles.Items.Remove(lbFiles.SelectedItems[i]);
                }
            }
        }

        private void AddAssetForm_Shown(object sender, EventArgs e)
        {
            lbFiles.Items.Clear();
            filePaths.Clear();
            fullFilePaths.Clear();
        }
        private void AddAssetForm_Load(object sender, EventArgs e)
        {
            lbFiles.Items.Clear();
            filePaths.Clear();
            fullFilePaths.Clear();
        }
    }
}
